package com.sjgl.esdn.controller;

import com.sjgl.esdn.constant.UserConstantsESDN;
import com.sjgl.esdn.domain.AjaxResultESDN;
import com.sjgl.esdn.domain.entity.User;
import com.sjgl.esdn.domain.entity.vo.KeyWordVo;
import com.sjgl.esdn.domain.entity.vo.OprtVo;
import com.sjgl.esdn.domain.entity.vo.TypeVo;
import com.sjgl.esdn.service.DeptService;
import com.sjgl.esdn.service.UserManageService;
import com.sjgl.esdn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户管理 控制层
 * @author 虾米
 */
@RestController
@RequestMapping("/manage")
public class UserManageController extends BaseControllerESDN {

    @Autowired
    private UserManageService userManageService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @GetMapping("/list")
    public AjaxResultESDN getUserList(Long userId){
        return AjaxResultESDN.success("success",userManageService.getUserList(userId));
    }

    @GetMapping("/deptUserList")
    public AjaxResultESDN getDeptUserList(String deptName){
        List<User> result = userManageService.getUserListByDeptName(deptName);
        if(result==null){
            return AjaxResultESDN.success("该部门暂无用户");
        }else {
            return AjaxResultESDN.success("success", userManageService.getUserListByDeptName(deptName));
        }
    }

    @GetMapping("/search")
    public AjaxResultESDN searchByKeyword(KeyWordVo vo){
        return AjaxResultESDN.success("success",userManageService.selectByKeyword("%"+vo.getKeyword()+"%",vo.getUserId()));
    }

    @GetMapping("/showApproval")
    public AjaxResultESDN showApproval(Long userId){
        return AjaxResultESDN.success("success",userManageService.showApproval(userId));
    }

    @RequestMapping("/approveRegister")
    public AjaxResultESDN approveRegister(@RequestBody OprtVo vo)throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(vo.getTime());
        int ajax = userManageService.approveRegister(vo.getUserId(),vo.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("success",ajax);
        }else{
            return AjaxResultESDN.error("FAIL",ajax);
        }
    }

    @GetMapping("/showResetPwd")
    public AjaxResultESDN showResetPwd(Long userId){
        return AjaxResultESDN.success("success",userManageService.showResetList(userId));
    }

    @RequestMapping("/onlyResetPwd")
    public AjaxResultESDN resetPwd(@RequestBody OprtVo vo) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(vo.getTime());
        int ajax = userManageService.resetPassword(vo.getUserId(),vo.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("success",ajax);
        }else{
            return AjaxResultESDN.error("FAIL",ajax);
        }
    }

    @RequestMapping("/aprvReset/pass")
    public AjaxResultESDN pass(@RequestBody OprtVo vo) throws ParseException{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(vo.getTime());
        userManageService.resetPassword(vo.getUserId(),vo.getOprtId(),date);
        int ajax = userManageService.updateStatus(vo.getUserId(),vo.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("success",ajax);
        }else{
            return AjaxResultESDN.error("FAIL",ajax);
        }
    }

    @RequestMapping("/aprvReset/reject")
    public AjaxResultESDN reject(@RequestBody OprtVo vo) throws ParseException{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(vo.getTime());
        int ajax = userManageService.rejectStatus(vo.getUserId(),vo.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("success",ajax);
        }else{
            return AjaxResultESDN.error("FAIL",ajax);
        }
    }

    @RequestMapping("/modifyUserType")
    public AjaxResultESDN modifyUserType(@RequestBody TypeVo tv)throws ParseException{

        //数据处理
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(tv.getTime());
        String type = tv.getUserType();
        int usertype;
        if(UserConstantsESDN.TOURIST.equals(type)){
            usertype = UserConstantsESDN.USER_TOURIST;
        }else if(UserConstantsESDN.EMPOLYEE.equals(type)){
            usertype = UserConstantsESDN.USER_EMPLOYEE;
        }else{
            usertype = UserConstantsESDN.USER_MANAGER;
        }

        User oprtUser=userService.selectUserById(tv.getOprtId());
        User user=userService.selectUserById(tv.getUserId());
        //判断是否有修改的权限
        if(oprtUser.getUserType()<=user.getUserType() || oprtUser.getUserType()<=usertype){
            return AjaxResultESDN.error("没有修改权限");
        }
        //若目标部门已有管理者，则修改失败
        if(usertype==UserConstantsESDN.USER_MANAGER && deptService.getLeaderId(deptService.getIdByDeptName(tv.getDeptName()))!=0){
            return AjaxResultESDN.error("目标部门已存在管理者，请先解除其管理权限");
        }

        //进行修改操作
        int ajax = userManageService.modifyUserType(tv.getUserId(),tv.getDeptName(),usertype,tv.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("修改成功",ajax);
        }else{
            return AjaxResultESDN.error("修改失败",ajax);
        }
    }

    @RequestMapping("/discard")
    public AjaxResultESDN discard(@RequestBody OprtVo vo)throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(vo.getTime());
        int ajax = userManageService.discardUser(vo.getUserId(),vo.getOprtId(),date);
        if(ajax>0){
            return AjaxResultESDN.success("success",ajax);
        }else{
            return AjaxResultESDN.error("FAIL",ajax);
        }
    }


}
